package service

import (
	set "github.com/deckarep/golang-set"
	"github.com/gogf/gf/errors/gerror"
	"github.com/gogf/gf/frame/g"
	"github.com/gogf/gf/net/ghttp"
	"shrimp-blog-server/app/dao"
	"shrimp-blog-server/app/model"
)

func (*SortsService) AddSorts(req *ghttp.Request, sorts ...model.SortsRequest) {
	if len(sorts) == 0 {
		responses.ErrorResponseExit(req, "请输入分类信息")
	}
	var sortModels []model.Sorts

	oldset := set.NewSet()
	for i := range sorts {
		oldset.Add(sorts[i].Name)
	}

	err := dao.Sorts.DB().Model(dao.Sorts.Table).Where(g.Map{dao.Sorts.Columns.Name: oldset.ToSlice()}).WhereNotNull(dao.Sorts.Columns.DeleteTime).Unscoped().Scan(&sortModels)
	if err != nil {
		panic(gerror.Wrap(err, "查询分类失败"))
	}
	if len(sortModels) > 0 {
		tx, err := dao.Sorts.DB().Begin()
		if err != nil {
			panic(gerror.Wrap(err, "开启事务失败"))
		}
		for i := range sortModels {
			sortModels[i].DeleteTime = nil
			sortModels[i].Status = model.ShowStatus
			_, err := dao.Sorts.DB().Model(dao.Sorts.Table).TX(tx).Where(g.Map{dao.Sorts.Columns.Id: sortModels[i].Id}).WhereNotNull(dao.Sorts.Columns.DeleteTime).Unscoped().Update(sortModels[i])
			if err != nil {
				g.Log("添加分类").Error(gerror.Wrap(err, "更新分类失败"))
			}
		}
		if err != nil {
			err := tx.Rollback()
			if err != nil {
				panic(gerror.Wrap(err, "回滚事务失败"))
			}
		} else {
			err := tx.Commit()
			if err != nil {
				panic(gerror.Wrap(err, "提交事务失败"))
			}
		}
		newSet := set.NewSet()
		for i := range sortModels {
			newSet.Add(sortModels[i].Name)
		}
		diffSet := oldset.Difference(newSet)
		if len(diffSet.ToSlice()) > 0 {
			sortsModel := make([]model.Sorts, 0)
			for i := range diffSet.ToSlice() {
				sortsModel = append(sortsModel, model.Sorts{Name: diffSet.ToSlice()[i].(string)})
			}
			affected, err := dao.Sorts.Insert(sortsModel...)
			if err != nil {
				g.Log("添加分类失败").Error(err)
				panic(gerror.Wrap(err, "添加分类失败"))
			}
			if affected == 0 {
				responses.ErrorResponseExit(req, "添加分类失败")
				req.ExitAll()
			}
		}
	} else {
		for i := range sorts {
			sortModels = append(sortModels, model.Sorts{Name: sorts[i].Name, Status: model.ShowStatus})
		}
		_, err := dao.Sorts.Insert(sortModels...)
		if err != nil {
			g.Log("添加分类失败").Error(err)
			responses.ErrorResponseExit(req, "添加分类失败")
		}
	}

	responses.SuccessResponse(req)
}
func (*SortsService) UpdateSorts(req *ghttp.Request, sorts ...model.SortsRequest) {
	if len(sorts) == 0 {
		responses.ErrorResponseExit(req, "请输入分类信息")
	}
	tx, err := dao.Sorts.DB().Begin()
	if err != nil {
		panic(gerror.Wrap(err, "开启事务失败"))
	}
	for i := range sorts {
		_, err := dao.Sorts.Update(tx, sorts[i].Convert())
		if err != nil {
			g.Log("修改主题失败").Error(err)
		}
	}
	if err != nil {
		err := tx.Rollback()
		if err != nil {
			panic(gerror.Wrap(err, "回滚事务失败"))
		}
	} else {
		err := tx.Commit()
		if err != nil {
			panic(gerror.Wrap(err, "提交事务失败"))
		}
	}
	responses.SuccessResponse(req)
}

func (*SortsService) DeleteSorts(req *ghttp.Request, id ...uint) {
	if len(id) == 0 {
		responses.ErrorResponseExit(req, "请输入分类id")
	}
	models := make([]model.Sorts, len(id))
	affected, err := dao.Sorts.Delete(models...)
	if err != nil {
		g.Log("删除分类失败").Error(err)
		panic(gerror.Wrap(err, "删除分类失败"))
	}
	if affected == 0 {
		responses.ErrorResponseExit(req, "删除分类失败")
	}
	responses.SuccessResponse(req)
}

func (*SortsService) GetSorts(req *ghttp.Request, id ...uint) {
	var sorts []model.Sorts
	var err error
	if len(id) > 0 {
		sorts, err = dao.Sorts.GetModelById(id...)
	} else {
		err = dao.Sorts.DB().Model(dao.Sorts.Table).Scan(&sorts)
	}
	if err != nil {
		g.Log("获取分类失败").Error(err)
		panic(gerror.Wrap(err, "获取分类失败"))
	}
	responses.Json(req, successCode, successMessage, sorts)
}
func (server SortsService) GetSortsPage(req *ghttp.Request, page model.Page) {
	result, err := dao.Sorts.GetModelsByPage(page.Page, page.Size)
	if err != nil {
		g.Log("查询分类失败").Error(err)
		panic(err)
	}
	if result == nil {
		responses.ErrorResponseExit(req, "分类列表为空")
	}
	sortsResponsePage := result[0].ConvertPage(result, page)
	sortsResponsePage.Page.Total, err = dao.Sorts.DB().Model(dao.Sorts.Table).Count()
	if err != nil {
		panic(gerror.Wrap(err, "统计数量失败"))
	}
	responses.Json(req, successCode, successMessage, sortsResponsePage)
}
